package com.cwx.mychat.controller;

import com.cwx.mychat.entity.constant.CodeConstant;
import com.cwx.mychat.entity.dto.RegisterDto;
import com.cwx.mychat.entity.enums.ExceptionBizCodeEnum;
import com.cwx.mychat.exception.BusinessException;
import com.cwx.mychat.service.UserInfoService;
import com.cwx.mychat.utils.R;
import com.cwx.mychat.utils.redis.RedisUtil;
import com.cwx.mychat.utils.stringUtils.StringUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wf.captcha.SpecCaptcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/account")
@Validated //校验
public class AccountController {


    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 产生验证码并存入redis
     * @return
     */
    @RequestMapping("/checkCode")
    public R checkCode(){
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        String verCode = specCaptcha.text().toLowerCase();
        String checkCodeKey = UUID.randomUUID().toString();
        redisUtil.set(CodeConstant.REDIS_KEY_CHECK_CODE + checkCodeKey, verCode, CodeConstant.REDIS_TIME);
        log.info("验证码是{}", verCode);
        String checkCodeBase64 = specCaptcha.toBase64();
        Map<String, Object> result = new HashMap<>();
        result.put("checkCode", checkCodeBase64);
        result.put("checkCodeKey", checkCodeKey);
        return R.ok().put(CodeConstant.RESULT_MSG, result);
    }

    /**
     * 注册
     * @return
     */
    @RequestMapping("/register")
    public R register(@RequestBody RegisterDto registerDto) throws BusinessException {
        try {
            userInfoService.regist(registerDto);
        }finally {
            redisUtil.del(CodeConstant.REDIS_KEY_CHECK_CODE + registerDto.getCheckCodeKey());
        }
        return R.ok();
    }

    /**
     * 获取4位数的手机验证码
     * @return
     */
    @GetMapping("/getPhoneCode")
    public R getPhoneCode(@RequestParam("phone") String phone) throws BusinessException {
        //随机产生4位数
        if(!StringUtil.isPhoneNumber(phone)){
            throw new BusinessException(ExceptionBizCodeEnum.PHONE_FORMAT_EXCEPTION);
        }
        Random rand = new Random();
        String randomNum = (rand.nextInt(9000) + 1000) + "";
        log.info("手机号{}的验证码是{}", phone, randomNum);
        String checkCodeKey = UUID.randomUUID().toString();
        redisUtil.set(CodeConstant.REDIS_KEY_CHECK_CODE + checkCodeKey, randomNum, CodeConstant.REDIS_TIME);
        Map<String, Object> result = new HashMap<>();
        result.put("checkCodeKey", checkCodeKey);
        result.put("checkCode", randomNum);
        return R.ok().put(CodeConstant.RESULT_MSG, result);
    }

    /**
     * 获取可能认识的人
     * @return
     */
    @GetMapping("/possibleContacts")
    public R getPossibleContacts(){
        return userInfoService.getPossibleContacts();
    }


    @RequestMapping("/test")
    public void test(HttpServletResponse response, HttpServletRequest request) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        String resultBody = "{\n" +
                "    \"Body\":{\n" +
                "        \"receiveDataInfoResponse\": {\n" +
                "            \"return\": \"<returnInfo><returnCode>0</returnCode><description>实时数据完成</description></returnInfo>\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        StringWriter sw = new StringWriter();

        try {
            mapper.writeValue(sw, resultBody);
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        resultBody = sw.toString();
        log.info("查出：" + resultBody);
        response.setHeader("Content-Type", "application/xml");
        response.getWriter().write(resultBody);
    }


}
